package www.xudan.android_ffmpeg.videoplayer.widget.status;

import android.media.MediaPlayer;
import android.view.View;


import tv.danmaku.ijk.media.player.IMediaPlayer;
import www.xudan.android_ffmpeg.R;
import www.xudan.android_ffmpeg.videoplayer.widget.BasePlayerView;

public class PlayingStatus extends BaseStatus {
    public PlayingStatus(BasePlayerView view) {
        super(view);
    }

    boolean isBack;

    @Override
    public void play() {
        view.player.pause();
        view.iv_screen_play.setVisibility(View.VISIBLE);
        view.seek_play.setImageResource(R.drawable.play);
        view.iv_screen_play.setImageResource(R.drawable.screen_play);
        view.setStatus(view.pause);
    }

    @Override
    public void error() {
        view.player.stop();
        view.iv_screen_play.setImageResource(R.drawable.error);
        view.iv_screen_play.setVisibility(View.VISIBLE);
        view.setStatus(view.error);
    }

    @Override
    public void completed() {
        if (view.isLooper) {
            view.player.start();
            view.removeLoading();
            view.setStatus(view.playing);
        } else {
            releasePlayer();
        }
    }

    @Override
    public void onInfo(IMediaPlayer iMediaPlayer, int what, int i1) {
        if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) {
            view.showLoading();
            view.setStatus(view.initing);
        }
    }

    @Override
    public void pause() {
        view.seek_play.setImageResource(R.drawable.play);
        view.iv_screen_play.setImageResource(R.drawable.screen_play);
        view.iv_screen_play.setVisibility(View.VISIBLE);
        view.player.pause();
        view.setStatus(view.pause);
    }

    @Override
    public void netChange() {

    }

    public void restoreFromPlayerView() {
        view.iv_video_pic.setVisibility(View.GONE);
        view.iv_screen_play.setVisibility(View.GONE);
        view.seek_play.setImageResource(R.drawable.pause);
        view.setStatus(view.playing);
    }

    @Override
    public void onBack() {
        isBack = true;
        view.seek_play.setImageResource(R.drawable.play);
        view.iv_screen_play.setImageResource(R.drawable.screen_play);
        view.iv_screen_play.setVisibility(View.VISIBLE);
        view.player.pause();
        view.setStatus(view.pause);
    }

    @Override
    public void onStart() {
        isBack = false;
    }

    @Override
    public void copyStatusToChild(BasePlayerView view) {
        view.setStatus(view.playing);
    }

    @Override
    public void onStartScrolled() {
        super.onStartScrolled();
        view.player.pause();
    }

    @Override
    public void onItemRollOut() {
        view.seek_play.setImageResource(R.drawable.play);
        view.iv_screen_play.setImageResource(R.drawable.screen_play);
        view.iv_screen_play.setVisibility(View.VISIBLE);
        view.player.pause();
        view.setStatus(view.pause);
    }

    @Override
    public void onEndScrolled() {
        super.onStartScrolled();
        view.removeLoading();
        view.player.start();
    }
}
